<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" :inline="true" label-width="89px" @keyup.enter.native="reload" @submit.native.prevent>
        <!--        <el-form-item label="选择时间:">-->
        <!--          <el-date-picker-->
        <!--            style="width: 100%"-->
        <!--            v-model="where.time_range"-->
        <!--            type="datetimerange"-->
        <!--            value-format="yyyy-MM-dd HH:mm:ss"-->
        <!--            label-format="yyyy-MM-dd HH:mm:ss"-->
        <!--            range-separator="至"-->
        <!--            start-placeholder="开始日期"-->
        <!--            end-placeholder="结束日期">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <el-form-item label="设备状态:">
          <el-select clearable v-model="where.running_status" placeholder="请选择设备状态" class="ele-fluid">
            <el-option label="空闲" value="0"/>
            <el-option label="运行中" value="1"/>
            <el-option label="挂机中" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="用户ID:">
          <el-input clearable v-model="where.member_id" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="用户启流ID:">
          <el-input clearable v-model="where.user_phone_id" placeholder="请输入用户启流设备id"/>
        </el-form-item>

        <el-form-item action="">
          <div class="ele-form-actions">
            <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
            </el-button>
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
                     height="calc(100vh - 315px)">
        <template slot="toolbar">
<!--          <el-button-->
<!--            size="small"-->
<!--            type="success"-->
<!--            icon="el-icon-download"-->
<!--            class="ele-btn-icon"-->
<!--            @click="exportExcel"-->
<!--            v-if="permission.includes('sys:box-yun-order:export')">导出-->
<!--          </el-button>-->
        </template>

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-popconfirm class="ele-action" title="确定要退出挂机吗？" @confirm="close(row)"
                         v-if="permission.includes('sys:box-yun-user-machine:close')">
            <el-link type="info" slot="reference" :underline="false">退出挂机
            </el-link>
          </el-popconfirm>

          <el-link type="success" :underline="false" @click="openEdit(row)"
                   v-if="permission.includes('sys:box-yun-user-machine:edit')">修改到期时间
          </el-link>

          <el-popconfirm class="ele-action" title="确定要退出挂机吗？" @confirm="recycle(row)"
                         v-if="permission.includes('sys:box-yun-user-machine:recycle')">
            <el-link type="danger" slot="reference" :underline="false">回收设备
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 修改到期时间 -->
    <edit :data="current" :visible.sync="showEdit" @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import edit from './edit';
import {downloadExcel} from "@/utils/excel-export"

export default {
  name: 'YunOrder',
  components: {edit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/BoxYunUserMachine/index',
      // 表格列配置
      columns: [
        {
          prop: 'id',
          label: '序号',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'user_device_id',
          label: '用户启流ID',
          minWidth: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'name',
          label: '设备别名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'member_id',
          label: '用户编号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'valid_start_time',
          label: '绑定时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120,
        },
        {
          prop: 'valid_end_time',
          label: '过期时间',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'status_name',
          label: '设备状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'running_status_name',
          label: '显示状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'onHook_status_name',
          label: '挂机状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'bind_status',
          label: '绑定状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          columnKey: 'action',
          label: '操作',
          minWidth: 180,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 下载数据Excel */
    exportExcel() {
      downloadExcel(`BoxYunUserMachine/export`, this.where)
    },

    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 退出挂机 */
    close(row) {
      console.log(1)
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "member_id": row.member_id,
        "user_device_id": row.user_device_id
      })
      this.$http.post('/BoxYunUserMachine/closeUserMachine', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 回收设备 */
    recycle(row) {
      console.log(1)
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "member_id": row.member_id,
        "user_device_id": row.user_device_id
      })
      this.$http.post('/BoxYunUserMachine/recycleUserMachine', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped></style>
